package com.example.styservice.service;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.FieldValue;
import co.elastic.clients.elasticsearch._types.Refresh;
import co.elastic.clients.elasticsearch.core.DeleteResponse;
import co.elastic.clients.elasticsearch.core.SearchRequest;
import co.elastic.clients.elasticsearch.core.SearchResponse;
import co.elastic.clients.elasticsearch.core.search.Hit;
import co.elastic.clients.elasticsearch.indices.DeleteIndexResponse;
import com.alibaba.dubbo.config.annotation.Service;
import com.example.styinterface.interfaces.IElasticSearchService;
import com.example.styinterface.utils.R;
import com.example.stymodel.model.Apple;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wcq
 */
@Component
@Service
public class ElasticSearchServiceImpl implements IElasticSearchService {

    @Autowired
    private ElasticsearchClient elasticsearchClient;

    public R addIndex(String index) {
        try {
            elasticsearchClient.indices().create(builder -> {
                builder.index("animal")
                        .settings(s -> s.numberOfShards("3").numberOfReplicas("0"));
                return builder;
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.success(200,"添加index成功");
    }

    public R deleteIndex(String index) throws IOException {

        DeleteIndexResponse deleteIndexResponse=elasticsearchClient.indices().delete(builder -> {
            builder.index("animal");
            return builder;
        });
        return R.success(200,"已删除索引");
    }

    public R insertDocument(String index,Apple apple) throws IOException {
        elasticsearchClient.index(builder -> {
            builder.index(index).document(new Apple()).id(apple.getId().toString())
                    .document(apple)
                    .refresh(Refresh.True);
            return builder;
        });
        return R.success(200,"添加文档成功");
    }

    public R deleteDocument(String index,Integer id)  throws IOException {
        DeleteResponse deleteResponse= elasticsearchClient.delete(builder -> {
            builder.index(index).id(id.toString()).refresh(Refresh.True);
            return builder;
        });
        return R.success(200,"已删除文档");
    }
    public R selectDocument(String index, String price) throws IOException {
        SearchRequest searchRequest = SearchRequest.of(s -> s
                .index(index)
                .query(q -> q
                        .bool(b -> b
                                .must(m -> m.term(t -> t.field("price").value(FieldValue.of(price))))
                        )
                ));
        SearchResponse<Apple> search = elasticsearchClient.search(searchRequest, Apple.class);
        List<Apple> collect = search.hits().hits().stream().map(Hit::source).collect(Collectors.toList());
        return R.success(collect,200,"查找成功");
    }
}
